package com.alibaba.algo.domain.user;

import com.alibaba.algo.cooperation.feign.vo.req.SelectGroupPageRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeGroupInfoVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeInfo;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.dao.user.SysEmployeeGroupMapper;
import com.alibaba.algo.entity.user.SysEmployeeGroup;
import com.alibaba.algo.entity.user.SysGroup;
import com.alibaba.algo.util.DateConverterUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类SysEmployeeGroup.java的实现描述：用户组关系表Domain
 *
 * @author fan.tianpeng 2021-05-06 20:30:48
 */
@Component
public class SysEmployeeGroupDomain extends BaseDomain<SysEmployeeGroup> {

    @Resource
    private SysEmployeeGroupMapper sysEmployeeGroupMapper;

    /**
     * 批量新增用户组和员工的关联关系
     *
     * @param groupId        用户组标识
     * @param employeeIdList 员工列表
     */
    public void batchInsert(Long groupId, List<Long> employeeIdList, String userName) {
        for (Long employeeId : employeeIdList) {
            insert(groupId, employeeId, userName);
        }
    }

    public void insert(Long groupId, Long employeeId, String userName) {
        LocalDateTime cutDateTime = DateConverterUtils.asLocalDateTime(new Date());
        SysEmployeeGroup sysEmployeeGroup = SysEmployeeGroup.builder().build();
        sysEmployeeGroup.setEmployeeId(employeeId);
        sysEmployeeGroup.setGroupId(groupId);
        sysEmployeeGroup.setIsDeleted(YesOrNo.N.name());
        sysEmployeeGroup.setGmtCreated(cutDateTime);
        sysEmployeeGroup.setGmtModified(cutDateTime);
        sysEmployeeGroup.setCreator(userName);
        sysEmployeeGroup.setModifier(userName);
        sysEmployeeGroupMapper.insert(sysEmployeeGroup);
    }


    /**
     * 查询全量的用户组员工
     *
     * @param groupId 用户组标识
     * @return 员工列表
     */
    public List<EmployeeInfo> selectEmployeeListByGroupCondition(
            Long groupId,
            String employeeName,
            String userAccount,
            String employeeCode,
            Long employeeId,
            String userName,
            Long userId,
            String searchKey) {
        return sysEmployeeGroupMapper.selectEmployeeListByGroupCondition(
                groupId,
                employeeName,
                userAccount,
                employeeCode,
                employeeId,
                userName,
                userId,
                searchKey);
    }

    public Integer countEmployeeListByGroupCondition(Long groupId) {
        return sysEmployeeGroupMapper.countEmployeeListByGroupCondition(groupId);
    }

    public List<EmployeeInfo> pageEmployeeByGroupId(Long groupId, Integer pageIndex, Integer pageSize) {
        return sysEmployeeGroupMapper.pageEmployeeByGroupId(groupId, pageIndex, pageSize);
    }

    public Integer pageEmployeeCountByGroupId(Long groupId) {
        return sysEmployeeGroupMapper.pageEmployeeCountByGroupId(groupId);
    }

    public List<EmployeeInfo> selectListWithGroupIdByGroupCondition(
            Long groupId,
            String employeeName,
            String userAccount,
            String employeeCode,
            Long employeeId,
            String userName,
            Long userId,
            String searchKey) {
        return sysEmployeeGroupMapper.selectListWithGroupIdByGroupCondition(
                groupId,
                employeeName,
                userAccount,
                employeeCode,
                employeeId,
                userName,
                userId,
                searchKey);
    }

    /**
     * 查询员工所在的用户组
     *
     * @param employeeId 员工标识
     * @return 用户组列表
     */
    public List<SysGroup> selectGroupListByEmployeeId(Long employeeId) {
        return sysEmployeeGroupMapper.selectGroupListByEmployeeId(employeeId);
    }

    public SysEmployeeGroup selectOneByEmployeeIdAndGroupId(Long employeeId, Long groupId) {
        return sysEmployeeGroupMapper.selectOneByEmployeeIdAndGroupId(employeeId, groupId);
    }

    /**
     * 查询用户组下的员工列表
     *
     * @param groupId 用户组标识
     * @return 员工列表
     */
    public List<EmployeeGroupInfoVO> selectEmployeeList(Long groupId) {
        return sysEmployeeGroupMapper.selectList(groupId);
    }

    public void deleteByGroupId(Long groupId) {
        sysEmployeeGroupMapper.deleteByGroupId(groupId);
    }

    public Integer countDirectEmployeeNumByGroupId(Long groupId) {
        return sysEmployeeGroupMapper.countDirectEmployeeNumByGroupCondition(groupId);
    }

    public List<SysGroup> selectGroupListByCondition(SelectGroupPageRequestVO selectGroupPageRequestVO) {
        return sysEmployeeGroupMapper.selectGroupListByCondition(selectGroupPageRequestVO);
    }

    public void updateByEmployeeIdGroupId(SysEmployeeGroup sysEmployeeGroup) {
        sysEmployeeGroupMapper.updateByEmployeeIdGroupId(sysEmployeeGroup);
    }

    public List<SysEmployeeGroup> selectEmployeeGroupListByGroupIds(List<Long> groupIdList, String isDeleted) {
        return sysEmployeeGroupMapper.selectEmployeeGroupListByGroupIds(groupIdList, isDeleted);
    }

    public void updateByEmployeeId(SysEmployeeGroup sysEmployeeGroup) {
        sysEmployeeGroupMapper.updateByEmployeeIdGroupId(sysEmployeeGroup);
    }

    public Integer updateByEmployeeIdLastModified(SysEmployeeGroup sysEmployeeGroup, LocalDateTime lastModifyTime) {
        return sysEmployeeGroupMapper.updateByEmployeeIdGroupIdLastModified(sysEmployeeGroup, lastModifyTime);
    }
}
